<?php

namespace app\api\controller\pifa;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Validate;
use think\Session;
use fast\WechatMini;
use addons\third\model\Third;
use think\Log;
use think\Request;
use fast\Http;
use think\Exception;
use addons\third\library\Service;
use fast\Fahuo;

/**
 * 首页接口
 */
class Peisong extends Api
{
    protected $noNeedLogin = ['login','getconfig','getMendian1','token','laobaninit'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        $this->token = $this->request->post('token');

        parent::_initialize();

    } 

  


     //添加修改收货地址
    public function daka(){

        $post = file_get_contents( "php://input");
        $post = json_decode($post,true);
        if (empty($post['fileList1'])) {
            $this->error('请上传打开图片');
        }
        if (empty($post['dingwei'])) {
            $this->error('请授权定位');
        }

        $images = [];
        if (!empty($post['fileList1'])) {
            foreach ($post['fileList1'] as $key => $value) {
               $images[$key] = $value['url'];
            }

        }
        $images = implode(',',$images);


        $map['id'] = $post['id'];
        $map['peisong_status'] = $post['peisong_status'];
        $map['orderstate'] = 2;

        $order = Db::table('fa_pifa_order')->where($map)->find();
        if (empty($order)) {
            $this->error('已打卡');
        }

        switch ($post['peisong_status']) {
            case '0':
                   $insdata['status'] = $post['peisong_status'];
                   $insdata['image'] = $images;
                   $insdata['address'] = "";
                   $insdata['latitude'] = $post['dingwei']['lat'];
                   $insdata['longitude'] = $post['dingwei']['long'];
                   $insdata['createtime'] = time();
                   Db::table('fa_pifa_orderdaka')->insert($insdata);
                   $updata['peisong_status'] = 1;
                   Db::table('fa_pifa_order')->where($map)->update($updata);
            break;

            case '1':
                   $insdata['status'] = $post['peisong_status'];
                   $insdata['image'] = $images;
                   $insdata['address'] = "";
                   $insdata['latitude'] = $post['dingwei']['lat'];
                   $insdata['longitude'] = $post['dingwei']['long'];
                   $insdata['createtime'] = time();
                   Db::table('fa_pifa_orderdaka')->insert($insdata);
                   $updata['peisong_status'] = 2;
                   $updata['orderstate'] = 2;
                   Db::table('fa_pifa_order')->where($map)->update($updata);
                   Fahuo::addFahuo($post['id']);
            break;
            
           
        }

        $this->success('打卡成功');

    }

    //接单
    public function myjiedan()
    {
        $post = $this->request->request();





        $map['id'] = $post['id'];
        $map['orderstate'] = 1;


        $updata['peisong_id'] = $this->auth->id;
        $updata['orderstate'] = 2;

        $res = Db::table('fa_pifa_order')->where($map)->update($updata);

        $this->success('接单成功');
    }


    //接单
    public function jiedan()
    {
        $post = $this->request->request();




        $map2['user_id'] = $this->auth->id;

        $info = Db::table('fa_pifa_peisongyuan')->where($map2)->find();

        if (empty($info)) {
           $this->error('无配送员信息，请联系库管');
        }

        $map['id'] = $post['id'];
        $map['orderstate'] = 1;
        $map['peisong_id'] = 0;

        $updata['peisong_id'] = $this->auth->id;
        $updata['orderstate'] = 2;

        $res = Db::table('fa_pifa_order')->where($map)->update($updata);

        $this->success('接单成功');
    }


    //拒绝
    public function jujue()
    {
        $post = $this->request->request();

        $map['id'] = $post['id'];
        $map['orderstate'] = 1;


        $map['peisong_id'] = $this->auth->id;
        $updata['peisong_id'] = 0;

        $res = Db::table('fa_pifa_order')->where($map)->update($updata);

        $this->success('拒绝成功');
    }

    
}
